from django.shortcuts import render
from django.views import View
from cartapp.cartmanager import getCartManger
from django.http import HttpResponseRedirect

# Create your views here.
class CartView(View):
    def post(self,request):
        # 在session中存取数据时使用到多级字典时需要实时更新
        request.session.modified = True
        # 获取用户当前操作类型变量
        flag = request.POST.get('flag','')
        if flag == 'add':
            cartManagerObj = getCartManger(request)
            cartManagerObj.add(**request.POST.dict())
        elif flag == 'plus':
            cartManagerObj = getCartManger(request)
            # step表示每次加多少
            cartManagerObj.update(step=1,**request.POST.dict())
        elif flag == 'minus':
            cartManagerObj = getCartManger(request)
            cartManagerObj.update(step=-1,**request.POST.dict())
        elif flag == 'delete':
            cartManagerObj = getCartManger(request)
            cartManagerObj.delete(**request.POST.dict())
        
        return HttpResponseRedirect('/cart/queryAll/')
    
class CartListView(View):
    def get(self,request):
        cartManagerObj = getCartManger(request)
        cartItemList = cartManagerObj.queryAll()

        return render(request,'cart.html',{'cartItemList':cartItemList})
    
